/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.data;

import java.io.Serializable;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.munchlevel.R;

public class GameInfo implements Serializable {

	private static final long serialVersionUID = 8193658414292516744L;

	private int players = 1;
	private int selectedPlayer = -1;

	private PlayerInfo[] player;

	public GameInfo() {

	}

	public void init(int players, Context context) {

		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

		this.players = players;

		if (player == null)
			player = new PlayerInfo[players];

		String playerName;
		for (int i = 0; i < players; i++) {

			if (players == 1) {
				playerName = "MunchLevel";
			} else {
				playerName = context.getResources().getText(R.string.label_player) + " " + (i + 1);
			}
			if (pref.contains(PlayerInfo.NAME + i))
				playerName = pref.getString(PlayerInfo.NAME + i, playerName);

			if (player[i] == null) {
				PlayerInfo info = new PlayerInfo();
				info.setName(playerName);
				info.setIndex(i);
				player[i] = info;
			}

		}

	}

	public void reset() {
		if (player != null) {
			for (PlayerInfo info : player) {
				if (info != null)
					info.reset();
			}
		}
	}

	public int getPlayers() {
		return players;
	}

	public void setPlayers(int player) {
		this.players = player;
	}

	public int getSelectedPlayer() {
		return selectedPlayer;
	}

	public PlayerInfo getSelectedPlayerInfo() {
		if (player != null && selectedPlayer >= 0)
			return player[selectedPlayer];
		else
			return null;
	}

	public void setSelectedPlayer(int selectedPlayer) {
		this.selectedPlayer = selectedPlayer;
	}

	public PlayerInfo[] getPlayer() {
		return player;
	}

	public PlayerInfo getPlayer(int i) {
		return player[i];
	}

	public void setPlayer(PlayerInfo[] player) {
		this.player = player;
	}

	public void setPlayer(int i, PlayerInfo player) {
		this.player[i] = player;
		player.setIndex(i);
	}

}
